/**
* @depends jquery.ui.js
*/

(function($) {

	$.widget( 'ui.window', {
		
		_init: function() {
		
			var $this = this;
			var objs = this.objs = {};
			objs.box = $('<div class="Box"></div>');
			objs.close = $('<a class="Delete Icon"></a>');
			objs.title = $('<h3>' + this.options.title + '</h3>');
			objs.title.append( objs.close );
			objs.box.append( objs.title );
			this.element.append( objs.box );
			objs.title.click( function() {
				$this.close();
			} );
		},

		
		open: function() {
			
			$('.Overlay').show();
			this.element.show();
			return this;
		},
		
		close: function() {
			if( this.element.hasClass( 'Removing' ) ) {
				return this;
			}
			this.element.addClass( 'Removing' );
			
			if( $('.ui-window:visible').length < 2 ) {
				$('.Overlay').fadeOut();
			}
			this.element.hide();
		},
		
		box: function() {
			return this.objs.box;
		},
		
		content: function( content ) {
			if( !content ) {
				return this.box();
			}
			this.box().append( content );
			return this;
		},
		
		options: {
			title: 'Untitled'
		}
		
	} );
	
	$.window = {
		create: function() {
			var div = $('<div class="Window ui-window"></div>');
			$(document.body).append( div );
			return div;
		}
	};
	
})(jQuery);
